package com.nondo.dean.auth.service.impl;

import com.nondo.dean.auth.domain.Menu;
import com.nondo.dean.auth.domain.MenuExample;
import com.nondo.dean.auth.mapper.MenuMapper;
import com.nondo.dean.auth.service.MenuService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findByParentId(Long parentId) {
        MenuExample example = new MenuExample();
        example.createCriteria().andParentIdEqualTo(parentId);
        example.setOrderByClause("parent_ids asc");
        return menuMapper.selectByExample(example);
    }

    @Override
    public List<Menu> findByParentIds(String parentIds) {

        MenuExample example = new MenuExample();
        example.createCriteria().andParentIdsLike(parentIds + "%");
        example.setOrderByClause("parent_ids asc");
        return menuMapper.selectByExample(example);
    }

    @Override
    public Menu findByMenuId(Long menuId) {
        return menuMapper.selectByPrimaryKey(menuId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByMenuId(Long menuId) {
        menuMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAndUpdateMenu(Menu menu) {
        //查询出数据库中是否存在当前菜单
        Menu oldMenu = menuMapper.selectByPrimaryKey(menu.getId());
        if (oldMenu == null) {
            menuMapper.insertSelective(menu);
        } else {
            menuMapper.updateByPrimaryKeySelective(menu);
        }
    }

    @Override
    public List<Menu> findAll() {
        MenuExample example = new MenuExample();
        example.setOrderByClause("parent_ids asc");
        return menuMapper.selectByExample(example);
    }

    @Override
    public Page<Menu> findPage(Query<Menu> menuQuery) {
        MenuExample example = new MenuExample();
        MenuExample.Criteria criteria = example.createCriteria();
        Menu item = menuQuery.getData();
        if (item != null) {
            if (item.getId() != null) {
                criteria.andIdEqualTo(item.getId());
            }
            if (item.getParentId() != null) {
                criteria.andParentIdEqualTo(item.getParentId());
            }
            if (StringUtils.isNotBlank(item.getName())) {
                criteria.andNameLike("%" + item.getName() + "%");
            }
        }

        example.setOrderByClause("parent_ids asc");

        List<Menu> resultList = menuMapper.selectByExampleWithRowbounds(example, new RowBounds(menuQuery.getOffset(), menuQuery.getLimit()));
        long total = menuMapper.countByExample(example);

        Page<Menu> resultPage = Page.of(menuQuery);
        resultPage.setTotalElements(total);
        resultPage.setContent(resultList);

        return resultPage;
    }

    @Override
    public List<Menu> findByUserId(Long userId) {
        return null;
    }

}
